extends IUsableComponent


class_name EquipableComponent


## 需要挂载的对象
@export var entry: Node = null
## 挂载点
@export var entry_point: String = ""

@export_category("Event")
## 需要修改IsEnable的触发器
@export var triggers: Array[IBasicComponent] = []
## 开启效果时触发的逻辑
@export var enter_actuator: IEventActuator = null
## 取消效果时触发的逻辑
@export var exit_actuator: IEventActuator = null
@export_category("")


var __entry_container: ItemContainer = null


func __prepare() -> void:
    super.__prepare()

    if not enter_actuator and not exit_actuator:
        var nodes: Array = IComponent.find_children(self, func(x): return x is IEventActuator, [])
        # 未指定时按照索引顺序设置
        if nodes.size() > 0:
            enter_actuator = nodes[0]
        if nodes.size() > 1:
            exit_actuator = nodes[-1]

## 应用效果
func apply(character: CharacterComponent) -> bool:
    if not super.apply(character):
        return false
    if entry:
        var container: ItemContainer = character.subassembly(entry_point)
        if container == null:
            push_error("apply ItemContainer subassembly error:", entry_point)
            super.cancel()
            return false
        container.append(entry)
        __entry_container = container
    for t in triggers:
        t.is_enable = true
    IEventActuator.safe_execute(enter_actuator)
    return true

## 取消效果
func cancel() -> void:
    IEventActuator.safe_execute(exit_actuator)
    for t in triggers:
        t.is_enable = false
    if entry and __entry_container:
        __entry_container.remove(entry)
    super.cancel()
